﻿using CefSharp;
using CefSharp.Handler;

namespace web_frame
{
    public delegate void LoadResponseDataEvent(byte[] data);
    class MyResourceHandler : ResourceRequestHandler
    {
        public event ResourceLoadCompleteEvent ResponseData;
        protected override bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
        {
            return false;
        }
        protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
        {
            MyResponseFilter filter = new MyResponseFilter();
            filter.LoadResponseData += LoadResponseData;
            return filter;
        }
        protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
        {
            ResponseData?.Invoke(request.Url, response.Charset, response.MimeType, response.StatusCode, response.StatusText, Data);
        }
        private byte[] Data = new byte[0];
        private void LoadResponseData(byte[] data)
        {
            byte[] arr = new byte[Data.Length + data.Length];
            Data.CopyTo(arr, 0);
            data.CopyTo(arr, Data.Length);
            Data = arr;
        }
    }
}
